package com.cloud.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MyListener implements ApplicationListener {

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        Object source = applicationEvent.getSource();
        String event = String.valueOf(source);
        //初始化不触发事物
        EventType eventType = EventType.toEnum(event);
        if(eventType != null){
            System.out.println("myEvent occured event : " + eventType.getDesc());
            HandlerEvents events = (HandlerEvents) applicationContext.getBean(eventType.getCode());
            try {
                events.push(applicationEvent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

/*    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("myEvent occured msg : " + contextRefreshedEvent.toString());
        System.out.println("myEvent occured source : " + contextRefreshedEvent.getSource());
        Object source = contextRefreshedEvent.getSource();
        String event = String.valueOf(source);
        System.out.println("myEvent occured event : " + event);

        EventType eventType = EventType.toEnum(event);
        if(eventType != null){
            HandlerEvents events = (HandlerEvents) applicationContext.getBean(eventType.name());
            events.push(contextRefreshedEvent);
        }


    }*/
}
